﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ATEC.GeometryNET;
using SSDPNET.Geom;
using ATEC.ATDataCoreNET;
using ATEC.ATDataCoreNET.ATObjects;
using DynamicButtons;

namespace GeometryExample
{

    public class CreateATBsplineSurfaceExample
    {
        public static ATSceneRef scene = SSDPNET.ScenesFunctions.SceneRefGetActive();
        /// <summary>
        /// 分类：玄镜BIM图形平台/C#开发/示例代码/几何/创建/创建B样条曲面。
        /// 功能：弹出五个按钮，分别演示不同方式创建B样条曲面.
        /// </summary>
        public static void CreateBsplineSurfacedMethod()
        {
            // 清空视口
            UseSomeBasicMethods.ClearScene();

            ButtonsView v = new ButtonsView();

            v.AddButton("B样条曲线转换成B样条曲面", (object sender, RoutedEventArgs e) =>
            {
                ATBsplineSurface surface1 = CreateSurfaced_Convert(); // 创建B样条曲面1
                // AddToSceneMethod.AddToScene_BsplineSurface(surface1, 1); // 将B样条曲面1添加到场景中（注释掉）
            });

            v.AddButton("通过两个基础线元构造B样条曲面", (object sender, RoutedEventArgs e) =>
            {
                ATBsplineSurface surface2 = CreateSurfaced_CreateRules(); // 创建B样条曲面2
                AddToSceneMethod.AddToScene_BsplineSurface(surface2, 2); // 将B样条曲面2添加到场景中
            });

            v.AddButton("通过基础线元和方向向量构造B样条曲面", (object sender, RoutedEventArgs e) =>
            {
                ATBsplineSurface surface3 = CreateSurface_FromBasic(); // 创建B样条曲面3
                AddToSceneMethod.AddToScene_BsplineSurface(surface3, 3); // 将B样条曲面3添加到场景中
            });

            v.AddButton("通过B样条曲线和方向向量构造B样条曲面", (object sender, RoutedEventArgs e) =>
            {
                ATBsplineSurface surface4 = CreateSurface_FromBsplineCurve(); // 创建B样条曲面4
                AddToSceneMethod.AddToScene_BsplineSurface(surface4, 4); // 将B样条曲面4添加到场景中
            });

            v.AddButton("通过CurveSet和方向向量构造B样条曲面集", (object sender, RoutedEventArgs e) =>
            {
                List<ATBsplineSurface> surfaces = CreateSurface_FromCurveSet(); // 创建B样条曲面集
                for (int i = 0; i < surfaces.Count; i++)
                {
                    AddToSceneMethod.AddToScene_BsplineSurface(surfaces[i], i + 5); // 将每个B样条曲面添加到场景中
                }
            });

            v.Show(); // 显示按钮视图
        }


        /// <summary>
        /// 分类：玄镜BIM图形平台/C#开发/示例代码/几何/创建/创建B样条曲面。
        /// 功能：通过两个基础线元构造B样条曲面.
        /// 关键类及说明：
        ///     XSegment3d: 线段，三维。
        ///     BasicCurve: 基本曲线，抽象类。可以表示多段线、弧（圆弧、椭圆）、螺旋线、B样条曲线等基本线型。
        ///     ATBsplineSurface: B样条曲面，三维。
        /// </summary>
        public static ATBsplineSurface CreateSurfaced_CreateRules()
        {
            // 通过两个BasicCurve创建B样条曲面
            XSegment3d seg = new XSegment3d(XPoint3d.Zero, new XPoint3d(100, 0, 0)); // 创建线段
            BasicCurve basicCrv = BasicCurve.CreateLine(seg); // 通过线段创建基本曲线
            BasicCurve basicCrvCopy = basicCrv.Clone(); // 复制基本曲线
            XTransform3d tranMoveTo = XTransform3d.FromTranslation(XVector3d.FromXYZ(0, 500, 0)); // 创建平移变换
            basicCrvCopy.Transform(tranMoveTo); // 对复制的基本曲线进行平移变换

            ATBsplineSurface bsplineSurface = ATBsplineSurfaceExtension.CreateRuled(basicCrv, basicCrvCopy); // 通过两个基础线元创建B样条曲面
            return bsplineSurface; // 返回创建的B样条曲面
        }

        /// <summary>
        /// 分类：玄镜BIM图形平台/C#开发/示例代码/几何/创建/创建B样条曲面。
        /// 功能：将对象转换为B样条曲面.
        /// 关键类及说明：
        ///     XPoint3d: 三维点.
        ///     ATBsplineCurve: B样条曲线.
        ///     BasicCurve: 基本曲线，抽象类。可以表示多段线、弧（圆弧、椭圆）、螺旋线、B样条曲线等基本线型。
        ///     ATBsplineSurface: B样条曲面，三维。
        ///     ATObject: 玄镜图形平台中的对象类。
        /// </summary>
        public static ATBsplineSurface CreateSurfaced_Convert()
        {
            // 将对象转换为B样条曲面
            XPoint3d pnt1 = new XPoint3d(0, 0, 0); // 创建点1
            XPoint3d pnt2 = new XPoint3d(100, 0, 0); // 创建点2
            XPoint3d pnt3 = new XPoint3d(200, 0, 100); // 创建点3
            XPoint3d pnt4 = new XPoint3d(200, 100, 100); // 创建点4
            XPoint3d pnt5 = new XPoint3d(100, 100, 0); // 创建点5
            XPoint3d pnt6 = new XPoint3d(0, 100, 0); // 创建点6
            List<XPoint3d> vPts = new List<XPoint3d>() { pnt1, pnt2, pnt3, pnt4, pnt5, pnt6, pnt1 }; // 创建点集合

            // 创建B样条曲线
            ATBsplineCurve bspline = ATBsplineCurveExtension.CreateFromInterpolationAtBasisFunctionPeaks(vPts, order: 3, 0); // 给定通过点，创建B样条曲线
            BasicCurve bcv = BasicCurve.CreateBsplineCurve(bspline); // 创建基本曲线
            ATObject curveObj = GeometryToATObject.ToObject(scene.GetATScene(), bcv, null); // 将基本曲线转换为玄镜图形平台对象
            curveObj.AddToScene(); // 将对象添加到场景中

            // 创建B样条曲面
            ATBsplineSurface bsplineSurface = ATBsplineSurfaceExtension.Create(); // 创建B样条曲面
            SSDPNET.ATSplineFunc.BsplineConvertToSurface(out bsplineSurface, curveObj); // 将对象转换为B样条曲面

            return bsplineSurface; // 返回创建的B样条曲面
        }


        /// <summary>
        /// 分类：玄镜BIM图形平台/C#开发/示例代码/几何/创建/创建B样条曲面。
        /// 功能：通过基础线元和方向向量构造B样条曲面.
        /// 关键类及说明：
        ///     BasicCurve: 基本曲线，抽象类。可以表示多段线、弧（圆弧、椭圆）、螺旋线、B样条曲线等基本线型。
        ///     XVector3d: 三维向量。
        ///     ATBsplineSurface: B样条曲面，三维。
        /// </summary>
        public static ATBsplineSurface CreateSurface_FromBasic()
        {
            BasicCurve basicCrv = CreateCurveSetMethods.CreateLineStringFromPts(); // 创建基础线元
            XVector3d vectorMoveTo = new XVector3d(0, 0, 500); // 创建方向向量
            ATBsplineSurface bsplineSurface = ATBsplineSurfaceExtension.CreateLinearSweep(basicCrv, vectorMoveTo); // 通过基础线元按指定方向向量扫掠形成B样条曲面
            return bsplineSurface; // 返回创建的B样条曲面
        }

        /// <summary>
        /// 分类：玄镜BIM图形平台/C#开发/示例代码/几何/创建/创建B样条曲面。
        /// 功能：通过B样条曲线和方向向量构造B样条曲面.
        /// 关键类及说明：
        ///     ATBsplineCurve: B样条曲线。
        ///     XVector3d: 三维向量。
        ///     ATBsplineSurface: B样条曲面，三维。
        /// </summary>
        public static ATBsplineSurface CreateSurface_FromBsplineCurve()
        {
            ATBsplineCurve bspline = CreateCurveSetMethods.CreateATBsplineCurves4(); // 创建B样条曲线
            XVector3d vectorMoveTo = new XVector3d(0, 0, 500); // 创建方向向量
            ATBsplineSurface bsplineSurface = ATBsplineSurfaceExtension.CreateLinearSweep(bspline, vectorMoveTo); // 通过B样条曲线，按指定方向向量扫掠形成B样条曲面
            return bsplineSurface; // 返回创建的B样条曲面
        }

        /// <summary>
        /// 分类：玄镜BIM图形平台/C#开发/示例代码/几何/创建/创建B样条曲面。
        /// 功能：通过CurveSet和方向向量构造B样条曲面集.
        /// 关键类及说明：
        ///     CurveSet: 曲线集，可以包含多个BasicCurve。
        ///     XVector3d: 三维向量。
        ///     ATBsplineSurface: B样条曲面，三维。
        /// </summary>
        public static List<ATBsplineSurface> CreateSurface_FromCurveSet()
        {
            CurveSet curveset = CurveSet.Create(CurveSet.BoundaryType.Open); // 创建曲线集
            {
                XPoint3d pt1 = XPoint3d.Zero; // 创建点1
                XPoint3d pt2 = new XPoint3d(100, 0, -100); // 创建点2
                XPoint3d pt3 = new XPoint3d(100, 50, -100); // 创建点3
                XPoint3d pt4 = new XPoint3d(0, 50, -100); // 创建点4

                BasicCurve basicCrv1 = BasicCurve.CreateLine(new XSegment3d(pt1, pt2)); // 创建线段1
                BasicCurve basicCrv2 = BasicCurve.CreateLine(new XSegment3d(pt2, pt3)); // 创建线段2
                BasicCurve basicCrv3 = BasicCurve.CreateLine(new XSegment3d(pt3, pt4)); // 创建线段3
                BasicCurve basicCrv4 = BasicCurve.CreateLine(new XSegment3d(pt4, pt1)); // 创建线段4

                curveset.Add(basicCrv1); // 将线段1添加到曲线集中
                curveset.Add(basicCrv2); // 将线段2添加到曲线集中
                curveset.Add(basicCrv3); // 将线段3添加到曲线集中
                curveset.Add(basicCrv4); // 将线段4添加到曲线集中
            }

            List<ATBsplineSurface> surfaces = new List<ATBsplineSurface>(); // 创建B样条曲面集列表
            XVector3d vectorMoveTo = new XVector3d(0, 0, -200); // 创建方向向量
            bool isSuccess = ATBsplineSurfaceExtension.CreateLinearSweep(out surfaces, curveset, vectorMoveTo); // 通过曲线集, 按指定方向向量扫掠形成B样条曲面
            return surfaces; // 返回创建的B样条曲面集
        }

        public class AddToSceneMethod
        {
            public static ATSceneRef scene = SSDPNET.ScenesFunctions.SceneRefGetActive();
            public static void AddToScene_BsplineSurface(ATBsplineSurface bsplineSurface, int color = 0)
            {
                if (bsplineSurface == null)
                {
                    return;
                }
                //显示B样条曲面
                SolidCoreEntity body;
                SSDPNET.DataCore.EntityUtl.Create.EntityFromBspSurface(out body, bsplineSurface, scene);

                if (body == null)
                {
                    return;
                }
                ATObject curveSetObj = GeometryToATObject.ToObject(scene.GetATScene(), body, null);
                ObjectPropertiesSetter setter = new ObjectPropertiesSetter();
                setter.SetColor((uint)color);
                setter.Apply(curveSetObj);
                curveSetObj.AddToScene();
            }

        }

        public class ATBSplineSurfaceMethodExample
        {
            public static void UseATBSplineSurfaceMethod()
            {
                //清空视口
                UseSomeBasicMethods.ClearScene();

                ButtonsView v = new ButtonsView();
                v.AddButton("获取v处未修剪u-方向曲线", (object sender, RoutedEventArgs e) =>
                {
                    //获取v处未修剪u-方向曲线
                    GetIsoVCurve();
                });

                v.AddButton("获取曲面上距离空间点最近的点", (object sender, RoutedEventArgs e) =>
                {
                    //获取曲面上距离空间点最近的点
                    CalClosetPoint();
                });

                v.AddButton("计算输入u和v参数值的B样条曲面点", (object sender, RoutedEventArgs e) =>
                {
                    //计算输入u和v参数值的B样条曲面点
                    EvaluatePoint();
                });

                v.AddButton("计算面积、质心、方向和主惯性矩", (object sender, RoutedEventArgs e) =>
                {
                    //计算面积、质心、方向和主惯性矩
                    CalSurfaceInfo();
                });
                v.Show();
            }

            /// <summary>
            /// 获取v处未修剪u-方向曲线
            /// </summary>
            public static void GetIsoVCurve()
            {
                ATBsplineSurface surface4 = CreateATBsplineSurfaceExample.CreateSurface_FromBsplineCurve();
                AddToSceneMethod.AddToScene_BsplineSurface(surface4, 1);

                //获取v处未修剪u-方向曲线
                {
                    ATBsplineCurve pSpline = surface4.GetIsoVCurve(0.5);
                    UseSomeBasicMethods.AddToScene_ATBsplineCurve(pSpline);
                }
            }

            /// <summary>
            /// 获取曲面上距离空间点最近的点
            /// </summary>
            public static void CalClosetPoint()
            {
                ATBsplineSurface surface = CreateATBsplineSurfaceExample.CreateSurfaced_Convert();
                AddToSceneMethod.AddToScene_BsplineSurface(surface, 2);
                XPoint3d spacePt = new XPoint3d(-50, -50, -50);
                UseSomeBasicMethods.AddToScene_Xpoint(spacePt, 2);
                //获取曲面上距离空间点最近的点
                {
                    XPoint3d surfacePt;
                    XPoint2d pt2d;
                    surface.ClosestPoint(out surfacePt, out pt2d, spacePt);
                    UseSomeBasicMethods.AddToScene_Xpoint(surfacePt);
                }
            }

            /// <summary>
            /// 计算输入u和v参数值的B样条曲面点
            /// </summary>
            public static void EvaluatePoint()
            {
                ATBsplineSurface surface = CreateATBsplineSurfaceExample.CreateSurfaced_CreateRules();
                AddToSceneMethod.AddToScene_BsplineSurface(surface, 3);
                //计算输入u和v参数值的B样条曲面点
                {
                    double u = 0.5;
                    double v = 0.5;
                    XPoint3d surfacePt;
                    surface.EvaluatePoint(out surfacePt, u, v);
                    UseSomeBasicMethods.AddToScene_Xpoint(surfacePt);
                }
            }

            /// <summary>
            /// 计算面积、质心、方向和主惯性矩
            /// </summary>
            public static void CalSurfaceInfo()
            {
                ATBsplineSurface surface = CreateATBsplineSurfaceExample.CreateSurface_FromBasic();
                AddToSceneMethod.AddToScene_BsplineSurface(surface, 4);
                //计算面积、质心、方向和主惯性矩
                {
                    double area;
                    XVector3d centroid;
                    XMatrix3d axes;
                    XVector3d momentxyz;
                    surface.ComputePrincipalAreaMoments(out area, out centroid, out axes, out momentxyz);

                    string msgDouble = "面积" + area.ToString("f3");
                    string msgCent = "质心" + centroid.X.ToString("f3") + "," + centroid.Y.ToString("f3") + "," + centroid.Z.ToString("f3");
                    string msgVec = "主惯性矩" + momentxyz.X.ToString("f3") + "," + momentxyz.Y.ToString("f3") + "," + momentxyz.Z.ToString("f3");
                    SSDPNET.ATDlgFuncs.DialogDmsgsPrint(msgDouble);
                    SSDPNET.ATDlgFuncs.DialogDmsgsPrint(msgCent);
                    SSDPNET.ATDlgFuncs.DialogDmsgsPrint(msgVec);
                }
            }
        }
    }
}
